package tutorial.java.dragdrop;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JCheckBoxMenuItem;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class FileChooserDemo extends JFrame {

	private JPanel contentPane;
	private JTextArea textArea;
	private JCheckBoxMenuItem chmnWrapLine;
	private JCheckBoxMenuItem cbmnWrapWord;
	private static final int FILE_OPEN = 1;
	private static final int FILE_SAVE = 2;
	private boolean k = false;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileChooserDemo frame = new FileChooserDemo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 
	 * @param title: name table of open or save file
	 * @param type: open or save
	 */
	private void operateFile(String title, int type){
		JFileChooser chooser = new JFileChooser();
		int choose = -1;
		chooser.setDialogTitle(title);
		switch (type) {
		case FILE_OPEN:
			choose = chooser.showOpenDialog(null);
			break;
		case FILE_SAVE:
			choose = chooser.showSaveDialog(null);
			break;
		}
		
		if (choose == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			switch (type) {
			case FILE_OPEN:
				readFile(file);
				this.setTitle(file.getName());
				break;
			case FILE_SAVE:
				if(k == true) {
					writeFile(file);
					this.setTitle("");
					k = false;
				} else {
					int result = JOptionPane.showConfirmDialog(null, "Do you want to save file ? ");
					if (result == JOptionPane.YES_OPTION){
						writeFile(file);
						this.setTitle(file.getName());
					}
				}
				
				break;
			}
		}
		
	}

	private void writeFile(File file) {
		// TODO Auto-generated method stub
		try {
			file.createNewFile();
			FileWriter fw = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(fw);
			String data = textArea.getText();
			bw.write(data);
			bw.close();
			fw.close();
//			JOptionPane.showMessageDialog(null, "File is saved");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void readFile(File file) {
		// TODO Auto-generated method stub
		textArea.setText("");
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line;
			while ((line = br.readLine()) != null) {
				textArea.append(line + "\n");
			}
			br.close();
			fr.close();
//			JOptionPane.showMessageDialog(null, "File is opened");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame.
	 */
	public FileChooserDemo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 453, 300);
		setLocationRelativeTo(null);
		setResizable(false);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmNew = new JMenuItem("New");
		mntmNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
//				JOptionPane.showMessageDialog(null, "New is updating");
				if (!textArea.getText().equals("")){
					int result = JOptionPane.showConfirmDialog(null, "Do you want save file ?");
					if (result == JOptionPane.YES_OPTION) {
						k = true;
						operateFile("Save File", FILE_SAVE);
					} else {
						textArea.setText("");
						textArea.requestFocus();
					}
				}
			}
		});
		mnFile.add(mntmNew);
		
		JMenuItem mntmOpen = new JMenuItem("Open");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				JOptionPane.showMessageDialog(null, "Open is updating");
				operateFile("Open File", FILE_OPEN);
			}
		});
		mnFile.add(mntmOpen);
		
		JMenuItem mntmSave = new JMenuItem("Save");
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				JOptionPane.showMessageDialog(null, "Save is updating");
				operateFile("Save File", FILE_SAVE);
			}
		});
		mnFile.add(mntmSave);
		
		JSeparator separator = new JSeparator();
		mnFile.add(separator);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnFile.add(mntmExit);
		
		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);
		
		cbmnWrapWord = new JCheckBoxMenuItem("Wrap Word");
		cbmnWrapWord.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				textArea.setWrapStyleWord(cbmnWrapWord.isSelected());
				textArea.setText(textArea.getText());
			}
		});
		mnEdit.add(cbmnWrapWord);
		
		chmnWrapLine = new JCheckBoxMenuItem("Wrap Line");
		chmnWrapLine.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				textArea.setLineWrap(chmnWrapLine.isSelected());
				textArea.setText(textArea.getText());
			}
		});
		mnEdit.add(chmnWrapLine);
		getContentPane().setLayout(null);
		
		textArea = new JTextArea();
		textArea.setDragEnabled(true);
		JScrollPane jScrollPane = new JScrollPane(textArea);
		jScrollPane.setBounds(0, 0, 437, 240);
		jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		getContentPane().add(jScrollPane);
		
	
	}
}
